#lang racket

(define (random-in-range low high)
  (let ((range (- high low)))
    (+ low (random range))))

(define (monte-carlo trials experiment)
  (define (iter trials-remaining trials-passed)
    (cond ((= trials-remaining 0)
           (/ trials-passed trials))
          ((experiment)
           (iter (- trials-remaining 1) (+ trials-passed 1)))
          (else
           (iter (- trials-remaining 1) trials-passed))))
  (iter trials 0))

(define (estimateintegral p x1 x2 y1 y2 trials)
  (let ((rational (monte-carlo 
                   trials
                   (lambda () (p
                               (random-in-range x1 x2)
                               (random-in-range y1 y2))))))
    (* rational (- x2 x1) (- y2 y1)))
  )

(estimateintegral (lambda (x y) false)
                  0
                  2
                  0
                  2
                  1000)